// 引入express模块
const express = require('express')
// 引入连接池模块
const pool = require('../pool.js')
// 创建路由对象
const shouye = express.Router()
// 首页分页数据查询接口

shouye.get(('/select'), (req, res, next) => {
    let classs = parseInt(req.query.classid)
    pool.query('select * from bindex where classid=?', [classs], (err, result) => {
        if (err) {
            next(err)
            console.log(err.message)
            return
        }
        console.log(result)
    })
    let page = parseInt(req.query.page ? req.query.page : 1)
    let pagesize = 6
    // 从第几条开始
    let setye = (page - 1) * pagesize
    // 一共几条数据
    let rowcount
    pool.query('select count(id) as count from bindex', (err, result) => {
        if (err) {
            next(err)
            return
        }
        // console.log(result)
        rowcount = result[0].rowcount
        // console.log(result)
        let pagecount = Math.ceil(rowcount / pagesize)
        sql = 'select * from bindex limit ?,?'
        pool.query(sql, [setye, pagesize], (err, result) => {
            if (err) {
                next(err)
                return
            }
            // console.log(result)
            res.send({
                code: 200,
                msg: '查询成功',
                result: result,
                pagecount: pagecount,
                page: page

            })
        })
    })
})
module.exports = shouye